package com.itlong.cloud.property.service.impl;


import com.itlong.cloud.POJO.DTO.property.PropertyProjectGetPropertyInfoBrowseStatusDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyProjectGetPropertyInfoDTO;
import com.itlong.cloud.POJO.PO.PropertyInfoBrowseStatusPO;
import com.itlong.cloud.POJO.PO.PropertyInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyProjectPropertyInfoBrowseStatusVO;
import com.itlong.cloud.POJO.VO.property.PropertyProjectPropertyInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyPropertyInfoBrowseStatusDao;
import com.itlong.cloud.property.dao.IPropertyPropertyInfoDao;
import com.itlong.cloud.property.service.IPropertyPropertyInfoService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <desc>
 *     物业投诉、物业报修、物业通知与便民信息 处理接口实现.
 * </desc>
 *
 * @createDate 2017-10-13
 */
@Service
public class PropertyPropertyInfoServiceImpl implements IPropertyPropertyInfoService{

    @Autowired
    IPropertyPropertyInfoDao iPropertyPropertyInfoDao;

    @Autowired
    IPropertyPropertyInfoBrowseStatusDao iPropertyPropertyInfoBrowseStatusDao;

    /**
     * <desc>
     *      单条查询 物业信息
     * <desc/>
     *
     * @param propertyInfoId 查询参数
     * @return
     * @author ZiXuan
     * @createDate 2017-12-01
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyProjectPropertyInfoVO getOnePropertyInfo(String propertyInfoId) throws Exception{
        PropertyProjectPropertyInfoVO propertyProjectPropertyInfoVO = iPropertyPropertyInfoDao.getOnePropertyInfo(propertyInfoId);
        return propertyProjectPropertyInfoVO;
    }

    /**
     * <desc>
     *      分页查询  物业投诉，物业报修，物业通知与便民信息
     * <desc/>
     *
     * @param pageDTO 分页查询物业信息DTO
     * @return
     * @author ZiXuan
     * @createDate 2017-10-16
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyProjectPropertyInfoVO> getPage(PropertyProjectGetPropertyInfoDTO pageDTO) throws Exception {
        //分页对象
        Page<PropertyProjectPropertyInfoVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO, page);

        //填充返回数据集
        page.setRows(iPropertyPropertyInfoDao.getSimplePage(params), iPropertyPropertyInfoDao.getCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存 物业信息。
     * <desc/>
     *
     * @param  saveDTO 请求参数
     * @return
     * @author ZiXuan
     * @createDate 2017-10-26
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer savePropertyInfo(PropertyProjectGetPropertyInfoDTO saveDTO) throws Exception{

        PropertyInfoPO propertyInfoPO = new PropertyInfoPO();
        BeanUtils.copyProperties(saveDTO, propertyInfoPO);
        Date createTime = new Date();
        propertyInfoPO.setPropertyInfoId(LogicIdUtil.bussinessId());
        propertyInfoPO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyInfoPO.setCreateTime(createTime);
        propertyInfoPO.setUpdateTime(createTime);
        propertyInfoPO.setVisitNum(0);
        Map<String, Object> params = SqlUtil.durableData(propertyInfoPO, PlatformConstants.TABLE_SAVE);
        return iPropertyPropertyInfoDao.save(params);
    }

    /**
     * <desc>
     *      获取投诉记录列表
     * </desc>
     *
     * @param projectId 项目编号
     * @param pageIndex 当前页数(每页15条)
     * @param userId    用户id
     * @return 投诉记录列表
     * @author wangzhi
     * @createDate 2019/06/17
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Map<String, Object> getComplaintList(String projectId, String pageIndex, String userId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        params.put("projectId",projectId);
        params.put("userId",userId);
        // 分页索引
        if (Integer.parseInt(pageIndex) > 0) {
            params.put("startSlip",(Integer.parseInt(pageIndex) - 1) * 15);
        } else {
            params.put("startSlip", 0);
        }
        // 分页记录数
        params.put("pageSize",15);
        List<Map<String, Object>> compAry = iPropertyPropertyInfoDao.getComplaintList(params);
        data.put("compAry",compAry == null ? new ArrayList<>() : compAry);
        return data;
    }

    /**
     * <desc>
     *      获取投诉详情
     * </desc>
     *
     * @param msgId 投诉记录ID
     * @return 投诉详情
     * @author wangzhi
     * @createDate 2019/06/17
     */
    @Override
    public Map<String, Object> getComplaintDetail(String msgId) throws Exception {
        // 查询投诉详情
        Map<String, Object> data = iPropertyPropertyInfoDao.getComplaintDetail(msgId);
        // 更新访问数
        PropertyInfoPO propertyInfoPO = new PropertyInfoPO();
        propertyInfoPO.setPropertyInfoId(msgId);
        String visitNum = data.get("visitNum") == null ? "0" : data.get("visitNum").toString();
        int visitNumInt = StringHandlerUtil.strToIntDef(visitNum, -1);
        propertyInfoPO.setVisitNum(visitNumInt == -1 ? 1 : visitNumInt + 1);
        propertyInfoPO.setUpdateTime(new Date());
        iPropertyPropertyInfoDao.update(SqlUtil.durableData(propertyInfoPO, PlatformConstants.TABLE_UPDATE));
        // 处理返回数据
        data.remove("visitNum");
        String imgUrl = data.get("imgUrl") == null ? "" : data.get("imgUrl").toString();
        data.remove("imgUrl");
        List<String> imageAry = StringHandlerUtil.splitStringList(imgUrl);
        data.put("imageAry", imageAry);
        return data;
    }

    /**
     * <desc>
     *      修改 物业信息。
     * <desc/>
     *
     * @param  updateDTO 请求参数
     * @return
     * @author ZiXuan
     * @createDate 2017-10-26
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updatePropertyInfo(PropertyProjectGetPropertyInfoDTO updateDTO) throws Exception{
        PropertyInfoPO propertyInfoPO = new PropertyInfoPO();
        BeanUtils.copyProperties(updateDTO, propertyInfoPO);
        propertyInfoPO.setCreateTime(null);//不允许更新创建时间
        propertyInfoPO.setUpdateTime(new Date());
        Map<String, Object> params = SqlUtil.durableData(propertyInfoPO, PlatformConstants.TABLE_UPDATE);
        return iPropertyPropertyInfoDao.update(params);
    }

    /**
     * <desc>
     *      删除物业信息。
     * <desc/>
     *
     * @param propertyInfoIds 物业信息ID列表
     * @return 返回影响行数
     * @author ZiXuan
     * @createDate 2017-11-02
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deletePropertyInfo(String propertyInfoIds) throws Exception{
        String dataId[] = StringHandlerUtil.splitString(propertyInfoIds);
        return iPropertyPropertyInfoDao.delete(dataId);
    }

    /**
     * <desc>
     *      分页查询  物业信息浏览状态
     * <desc/>
     *
     * @param pageDTO 分页查询物业信息DTO
     * @return
     * @author ZiXuan
     * @createDate 2017-12-21
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyProjectPropertyInfoBrowseStatusVO> getPropertyInfoBrowseStatusPage(PropertyProjectGetPropertyInfoBrowseStatusDTO pageDTO) throws Exception {
        //分页对象
        Page<PropertyProjectPropertyInfoBrowseStatusVO> page = new Page<>();
        //将DTO转换为Map
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(pageDTO, page);

        //填充返回数据集
        page.setRows(iPropertyPropertyInfoBrowseStatusDao.getSimplePage(params), iPropertyPropertyInfoBrowseStatusDao.getCount(params));
        return page;
    }

    /**
     * <desc>
     *      保存 物业信息浏览状态
     * <desc/>
     *
     * @param  saveDTO 请求参数
     * @return
     * @author ZiXuan
     * @createDate 2017-12-21
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer savePropertyInfoBrowseStatus(PropertyProjectGetPropertyInfoBrowseStatusDTO saveDTO) throws Exception{

        PropertyInfoBrowseStatusPO propertyInfoBrowseStatusPO = new PropertyInfoBrowseStatusPO();
        BeanUtils.copyProperties(saveDTO, propertyInfoBrowseStatusPO);
        Date createTime = new Date();

        propertyInfoBrowseStatusPO.setStatus(DataStatusEnum.NORMAL.getType());
        propertyInfoBrowseStatusPO.setCreateTime(createTime);
        propertyInfoBrowseStatusPO.setUpdateTime(createTime);
        Map<String, Object> params = SqlUtil.durableData(propertyInfoBrowseStatusPO, PlatformConstants.TABLE_SAVE);
        return iPropertyPropertyInfoBrowseStatusDao.save(params);
    }

}
